import time
import streamlit as st
from besins_selenium_downloaders.scripts import *
from besins_selenium_downloaders.config import *
from besins_selenium_downloaders.file_manager import *
from besins_selenium_downloaders.streamlit_tools import configure_page_navigation
import glob
import os
from datetime import datetime

class RunRemainingWebsites:
    def __init__(self):
        self.setup_page_config()
        configure_page_navigation("run_remaining_websites")
        st.markdown(
            '<h1 class="common-title">🔄 Run Remaining Websites</h1>',
            unsafe_allow_html=True,
        )
        self.main()

    def setup_page_config(self):
        st.set_page_config(
            page_title="Run Remaining Websites",
            page_icon="🔄",
            layout="wide",
            initial_sidebar_state="expanded",
        )

        # Load and inject CSS
        common_css_path = os.path.join(os.path.dirname(__file__), "../css/common.css")
        with open(common_css_path) as f:
            st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)

        css_path = os.path.join(os.path.dirname(__file__), "../css/run_remaining_websites.css")
        with open(css_path) as f:
            st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)

    def main(self):
        # Get today's date as folder path (can be empty)
        folder_path = datetime.now().strftime("%Y-%m-%d") 
        df = get_date_folder_analysis_as_dataframe(folder_path)

        # Debug: Check if the dataframe is empty or not
        if df.empty:
            st.warning("The data frame is empty. Please check your data source.")
        else:
            st.write(df.head())  # Show some rows to verify the content

        # Get all website names
        all_sites = get_website_names()
        completed_sites = set(df["SiteName"].unique()) if not df.empty else set()
        pending_sites = list(set(all_sites) - completed_sites)
        
        # Debug: Print out the number of all sites, completed sites, and pending sites
        st.write(f"Total Sites: {len(all_sites)}, Completed Sites: {len(completed_sites)}, Pending Sites: {len(pending_sites)}")

        # Check if there are pending sites
        if not pending_sites:
            st.warning("All websites have been processed. There are no remaining websites to run.")

        # Create columns for site management
        st.markdown("### 🎯 Sites to Download")

        # Prepare site options with SiteName | TaskID | URL
        site_task_map = map_site_to_task_id()
        site_url_map = map_site_to_url()

        site_options = [
            f"{site} | {site_task_map.get(site, 'TASK00000000')} | {site_url_map.get(site, 'N/A')}"
            for site in pending_sites
        ]

        # Sorting options
        sort_option = st.radio(
            "Sort Sites By",
            options=["Site Name", "Task ID"],
            index=0,
            horizontal=True,
        )

        # Sort the sites based on the selected option
        if sort_option == "Site Name":
            sorted_sites = sorted(site_options, key=lambda x: x.split(" | ")[0].lower())
        else:
            sorted_sites = sorted(site_options, key=lambda x: x.split(" | ")[1])

        # Set default selection to all pending sites
        default_selection = [
            f"{site} | {site_task_map.get(site, 'TASK00000000')} | {site_url_map.get(site, 'N/A')}"
            for site in all_sites if site not in completed_sites
        ]

        # Multiselect for site selection
        selected_sites = st.multiselect(
            "Select Websites to Download. Note: Websites that have already been downloaded will not appear in this list.",
            options=sorted_sites,
            default=default_selection,  # Default to all pending sites
            help="Choose the websites you want to download. Note: Websites that have already been downloaded will not appear in this list.",
        )

        # Start button
        if st.button(
            f"🚀 Start Processing {len(selected_sites)} Remaining Sites",
            type="primary",
            key="start_processing_button",
            help="Click to start processing the selected sites"
        ):
            # Process the selected sites
            total_sites = len(selected_sites)
            progress_bar = st.progress(0)
            status_text = st.empty()

            for i, site in enumerate(selected_sites, 1):
                status_text.markdown(
                    f"Processing site {i}/{total_sites}: **{site}**"
                )

                try:
                    run_one_website(site.split(" | ")[0])  # Extract the site name
                    st.success(f"✅ Successfully processed {site}")

                    # Update progress
                    progress_bar.progress(i / total_sites)

                except Exception as e:
                    st.error(f"❌ Error processing {site}: {str(e)}")
                    st.exception(e)

            status_text.markdown("✨ Processing completed!")


if __name__ == "__main__":
    RunRemainingWebsites()
